<?

include '../includes/specs_functions.php';
include "../../includes/feed_class.php";
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME = $_POST["THEME"];
$CLEANNAME = $SESSION->GetCharacter('cleanname');
$NAME = $SESSION->GetCharacter('name');
$AVATAR = $SESSION->GetCharacter('avatar');
#---------------------------------------------------



$output = json_encode(array("status"=>"error", "message"=>"NO"));

$specId = $_POST['specId'];
$comment = $_POST['comment'];

if($CICUID != "" && isset($_POST['specId']) && isset($_POST['comment'])){

	$table = "(".SPEC_TBL_SPECS." as spec
			LEFT JOIN 
			".TBL_CHARACTERS." as user
			ON user.".TBL_CHARACTERS_CICUID." = spec.".SPEC_TBL_SPECS_USER_ID.")";
	$col = array("spec.".SPEC_TBL_SPECS_USER_ID." as userId"
				,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
				,"user.".TBL_CHARACTERS_NAME." as Name"
				);
	$where = array("spec.".SPEC_TBL_SPECS_ID." = \"".$specId."\"");
	$orderBy = "spec.".SPEC_TBL_SPECS_USER_ID;
	$direction = "ASC";
	
	$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction);
	
	
	foreach ($result[3] as $row) {
		$padCICUID = $row['userId'];
		$specJob = GetClass($row['jobId']);
		$padUserName = $row['Name'];
	}
	
	$table = SPEC_TBL_COMMENTS;
	$data = array(
					SPEC_TBL_COMMENTS_USER_ID ."= \"".$CICUID."\""
					,SPEC_TBL_COMMENTS_SPEC_ID ."= \"".$specId."\""
					,SPEC_TBL_COMMENTS_COMMENT ."= \"".$comment."\""
					,SPEC_TBL_COMMENTS_CDATE ."= \"".date('Y-m-d H:i:s')."\""
					,SPEC_TBL_COMMENTS_UDATE ."= \"".date('Y-m-d H:i:s')."\""
				);
	
	$result = $DB->Insert($table ,$data);

	
	if ($result[0][1] == 0){
	
		if($padCICUID != $CICUID){
			$DB->Insert(TBL_MESSAGE,
					array(TBL_MESSAGE_TO 		."='".$padCICUID."'",	
						TBL_MESSAGE_FROM 		."='". $CICUID ."'",
						TBL_MESSAGE_ID 			."='". Random32() ."'",
						TBL_MESSAGE_TIME 		."='". time() ."'",
						TBL_MESSAGE_IPLONG 		."='". ip2long($_SERVER['REMOTE_ADDR']) ."'",
						TBL_MESSAGE_SUBJECT 	."='Specs: New Comment!'",
						TBL_MESSAGE_MESSAGE 	."=\"Dear ".$padUserName."<br><strong>".$NAME."</strong> commented on your <strong>".GetClass($row['jobId'])."</strong> spec:<br><br>".$comment."<br><br>XIVPads\"",
						TBL_MESSAGE_STATUS 		."='1'",
						TBL_MESSAGE_RESOLVED 	."='0'",
						TBL_MESSAGE_TYPE 		."='s_comment'"));
						
						
			$DB->Insert(TBL_PAD_SHOUT,
					array(TBL_PAD_SHOUT_PAD 		."='".$padCICUID."'",	
						TBL_PAD_SHOUT_CICUID 		."='". $CICUID ."'",
						TBL_PAD_SHOUT_CHARACTER 	."='". $CLEANNAME ."'",
						TBL_PAD_SHOUT_USER 			."='". time() ."'",
						TBL_PAD_SHOUT_IPLONG 		."='". ip2long($_SERVER['REMOTE_ADDR']) ."'",
						TBL_PAD_SHOUT_MESSAGES 		."=\" [color=#000000][size=10px][b]Commented on [color=#4380F9]".$padUserName."[/color] 's [color=#4380F9]".$specJob."[/color] spec:<br>[/b] ".$comment." [/size][/color] \"",
						TBL_PAD_SHOUT_DATE 			."='". time() ."'",
						TBL_PAD_SHOUT_NOTIFICATION ."='1'"));
				
			$Feed = new Feed($DB);
			/* PV SUCKS REALLY HARD !!!!!! */
			$Message = '<strong><a href="?'. $CICUID .'">'. $NAME .'</a></strong> Commented on <a href="?'. $padCICUID .'">'. $padUserName .'</a>&#39;s <a href="?Specs&'. $specId .'">'.GetClass($row['jobId']).'</a> spec.';
			$Feed->Add($CICUID, $NAME, $AVATAR, 's_comment', $Message);
		}
				
		$output = json_encode(array("status"=>"success", "message"=>"INSERT"));
		
		
	}
}

echo $output; 
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);
?>